package com.chen.project.file_server.utils;

import java.sql.*;


/**
 * 操作数据库的工具类
 *
 * @author AUSA
 */
public class JDBCUtils {


    /**
     * 获取数据库的连接
     *
     * @return 数据库连的接
     */
    public static Connection getConnection() throws ClassNotFoundException, SQLException {



        Connection conn = null;

        try {
//            // 1.读取配置文件中的4个基本信息
//            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("application.properties");
//            Properties pros = new Properties();
//            pros.load(is);
//            String user = pros.getProperty("user");
//            String password = pros.getProperty("password");
//            String url = pros.getProperty("url");
//            String driver = pros.getProperty("driver");


            String URL="jdbc:mysql://127.0.0.1:3306/study?useUnicode=true&characterEncoding=utf-8";
            String USER="root";
            String PASSWORD="123456";
            //1.加载驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");


            // 3.获取连接
            conn=DriverManager.getConnection(URL, USER, PASSWORD);

            // 关闭自动提交
            // conn.setAutoCommit(false);


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return conn;
    }


    /**
     * 关闭连接和Statement的操作
     *
     * @param conn
     * @param ps
     */
    public static void closeResource(Connection conn, Statement ps) {
        try {
            if (ps != null) {
                ps.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 关闭资源操作
     *
     * @param conn
     * @param ps
     * @param rs
     */
    public static void closeResource(Connection conn, Statement ps, ResultSet rs) {
        try {
            if (ps != null) {
                ps.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // // 程序停止前，应先关闭 derby
    // public static void closeDerby() {
    //     try {
    //         // // 关闭某个数据库
    //         // DriverManager.getConnection("jdbc:derby:file_db;shutdown=true");
    //         // 关闭 derby
    //         DriverManager.getConnection("jdbc:derby:;shutdown=true");
    //     } catch (SQLException se) {
    //         if (((se.getErrorCode() == 50000) && ("XJ015".equals(se.getSQLState())))) {
    //             System.out.println("Derby shut down normally");
    //         } else {
    //             System.err.println("Derby did not shut down normally");
    //             printSQLException(se);
    //         }
    //     }
    // }

    // private static void printSQLException(SQLException e) {
    //     while (e != null) {
    //         System.err.println("\n----- SQLException -----");
    //         System.err.println("  SQL State:  " + e.getSQLState());
    //         System.err.println("  Error Code: " + e.getErrorCode());
    //         System.err.println("  Message:    " + e.getMessage());
    //         e = e.getNextException();
    //     }
    // }
}

